#pragma once
#include <jsoncpp/json/json.h>
#include <sstream>
#include "logger.hpp"
/**************
 * 这是用于序列化和
 * 反序列化的接口
 **************/
class JSON
{
public:
    static bool serialize(const Json::Value &val, std::string &body)
    {
        Json::StreamWriterBuilder builder;
        std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
        std::stringstream ss;
        if (writer->write(val, &ss) != 0)
        {
            LOG(ERROR, "serialize fail");
            return false;
        }
        body = ss.str();
        return true;
    }
    static bool unserialize(Json::Value &val, const std::string &body)
    {
        Json::CharReaderBuilder builder;
        std::string errs;
        std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
        if (!reader->parse(body.c_str(), body.c_str() + body.size(), &val, &errs))
        {
            LOG(ERROR, "unserialize faile ,err_info:%s", errs.c_str());
            return false;
        }
        return true;
    }
};